<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\common\util\TreeUtil;
use \think\Db;

/**
 * 文章管理
 * @author xiegaolei
 *
 */
class Article extends Base
{	
	 
    /**
     * 文章列表
     */
    public function index(){
    
    	if ($this->request->isPost()){
    		$where= true;
    		//搜索
    		$search = input('search/a','');
    		if (!empty($search['value'])) {
    			$searchStr = html_entity_decode($search['value']);
    			parse_str($searchStr,$search);
    			$cid = $search['cid'];
    			$isRecommend = $search['isRecommend'];
    			$isShow = $search['isShow'];
    			$keyword = trim($search['keyword']);
    			
    			if ($cid != -1) {
    				$where .= ' and cid = '.$cid . ' ';
    			}
    			
    			if ($isShow != -1) {
    				$where .= ' and a.isShow = '.$isShow . ' ';
    			}
				
				if ($isRecommend != -1) {
    				$where .= ' and a.isRecommend = '.$isRecommend . ' ';
    			}
    		
    			if ($keyword != '') {
    				$where .= ' and a.title like "%'.$keyword.'%" ';
    			}
    		}
    		
    		$draw = input('draw',1,'intval');
    		$start = input('start',0,'intval');
    		$length = input('length',10,'intval');
    		//排序设置
    		$mycolumns = input('mycolumns/a','');
    		$myorder = input('order/a','');
    		if (empty($myorder)||empty($mycolumns)) {
    			$order = 'id asc';
    		}else{
    			$order = '';
    			foreach ($myorder as $key => $v) {
    				$order .= $mycolumns[$v['column']].' '.$v['dir'].' ,';
    			}
    			$order = rtrim($order , ',');
    		}
     
    		$db = Db::name('article');
    		$lists = $db->alias('a')
    				->where($where)
    				->field('a.*,b.title as ctitle')
    				->order($order)
    				->limit($start,$length)
    				->join('__COLUMN__ b ',' a.cid = b.id','left')
    				->select();
    		foreach ($lists as $key => $v){
    			if ($v['isShow']){
    				$lists[$key]['isShow'] = '正常';
    			}else{
    				$lists[$key]['isShow'] = '禁用';
    			}
				
				if ($v['isRecommend']){
    				$lists[$key]['isRecommend'] = '推荐';
    			}else{
    				$lists[$key]['isRecommend'] = '不推荐';
    			}
    			
    			$lists[$key]['addTime'] = date('Y-m-d H:i:s',$v['addTime']);
    		}
    		
    		$result['draw'] = $draw;
    		$result['recordsTotal'] = $db->count();
    		$result['recordsFiltered'] = $db->alias('a')->where($where)->count();
    		$result['data'] = $lists;
    		 
    		json_return($result);
    		 
    	}else{
    		
    		$column = Db::name('column')->where('isShow',1)->order('sorts','asc')->select();
    		//格式化处理
    		$column = TreeUtil::listToTreeOne( $column ,  0 , '|— ' , 'id' , 'pid' , 'html');
    		$this->assign('column', $column);
    		
    		return $this->fetch();
    	}
    }
    
    
    
    /**
     * 添加
     */
    public function articleAdd(){
    
    	$column = Db::name('column')->where('isShow',1)->order('sorts','asc')->select();
    	//格式化处理
    	$column = TreeUtil::listToTreeOne( $column ,  0 , '|— ' , 'id' , 'pid' , 'html');
    	$this->assign('column', $column);
    	
    	return $this->fetch();
    }
    
    /**
     * 编辑
     */
    public function articleEdit(){
    
    	$id = input('id','0','int');
    	if (empty($id)){
    		exit('参数有误！');
    	}
    	
    	$column = Db::name('column')->where('isShow',1)->order('sorts','asc')->select();
    	//格式化处理
    	$column = TreeUtil::listToTreeOne( $column ,  0 , '|— ' , 'id' , 'pid' , 'html');
    	$this->assign('column', $column);
    	
    	$info = Db::name('article')->where('id',$id)->find();
    	$this->assign('info',$info);
    	
    	return $this->fetch();
    }
    
    /**
     * 保存
     */
    public function articleSave(){
    	
    	//验证规则
    	$rule = [
    			'cid'  => ['require','gt'=>'0'],
    			'title'  => ['require'],
    	];
    	
    	//提示信息
    	$msg = [
    			'cid' => '请选择栏目',
    			'title' => '名称必填',
    	];
    	//接收数据
    	$data = [
    			'cid'  => input('cid',0,'int'),
    			'title'  => input('title','','trim'),
    			'descript'  => input('descript','','trim'),
    			'img'  => input('img','','trim'),
    			'isShow' => input('isShow','','trim'),
    			'content' => input('content'),
    			'addTime' => input('addTime','','strtotime')
    	];
    	
    	//验证输入数据合法性
    	$validate = new \think\Validate($rule, $msg);
    	$result   = $validate->check($data);
    	if (!$result){
    		$error = $validate->getError();
    		ajax_return_error($error);
    	}
    	
    	$id = input('id','0','int');

    	$article = Db::name('article');
    	if ($id){
    		 
    		$res = $article->where('id',$id)->update($data);
    	}else{
    		 
    		$res = $article->insert($data);
    	}
    	 
    	if ($res){
    		ajax_return_ok([],'操作成功！');
    	}else{
    		ajax_return_error('操作失败！');
    	}
    	 
    }
    
     
    
    /**
     * 删除
     */
    public function articleDel(){
    	$id = input('id','0','int');
    	if ($id == 0) {
    		exit('参数有误！');
    	}else{
    		
    		if (Db::name('comment')->where('aid',$id)->find()){
    			ajax_return_error('请先删除该文章下的评论！');
    		}
    		
    		$img = Db::name('article')->where('id',$id)->value('img');
    		//相对index.php 修正路径
    		$img = str_replace('/tp5/public', '.', $img);
    		if(file_exists($img)){
    			$exts = config('system.thumb_image_ext');
    			foreach ($exts as $v){
    				unlink($img.$v['width'].'_'.$v['height'].$v['ext']);
    			}
    			unlink($img);
    		}
    	
    		if (Db::name('article')->where('id',$id)->delete()) {
    			ajax_return_ok([],'操作成功！');
    		}else{
    			ajax_return_error('操作失败！');
    		}
    		 
    	}
    }
     
}
